package com.songlanyun.modules.refillorder.controller.admin;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.songlanyun.common.annotation.AuthIgnore;
import com.songlanyun.common.utils.DateUtils;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.utils.UUIDUtil;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.model.vo.AccountVO;
import com.songlanyun.modules.refillorder.entity.SaleRefillCardOrderEntity;
import com.songlanyun.modules.refillorder.model.RefillCardOrderVO;
import com.songlanyun.modules.refillorder.service.SaleRefillCardOrderService;
import com.songlanyun.modules.shop.service.ShopService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.activation.MimetypesFileTypeMap;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 充值订单
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-16 14:26:32
 */
@RestController
@RequestMapping("admin/order/refillcard")
public class SaleRefillCardOrderAdminController extends AbstractController {
    @Resource
    private SaleRefillCardOrderService saleRefillCardOrderService;

    @Resource
    private ShopService shopService;

    /**
     * 平台查询所有的充值订单
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = saleRefillCardOrderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 商家查询自己会员的充值卡订单
     */
    @GetMapping("/myRefillOrder")
    public R info(@RequestParam Map<String, Object> params) {
        Long uid = shopService.getUserIdByAdminId(getUserId(),false);//会员id
        params.put("user_id", uid);

        PageUtils page = saleRefillCardOrderService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 详情
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SaleRefillCardOrderEntity saleRefillCardOrder = saleRefillCardOrderService.getById(id);

        return R.ok().put("data", saleRefillCardOrder);
    }


    /**
     * 平台下
     * 充值订单导出
     */
    @GetMapping("/export")
    @AuthIgnore
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws IOException {
        Long adminId = this.getAdminByToken(response);//校验token

        List<RefillCardOrderVO> orderList = saleRefillCardOrderService.exportList(params);

        String fileName = "充值订单列表" + DateUtils.format(new Date(), "yyyy-MM-dd-HHmmss") + "_" + adminId + "." + "xls";
        ExportParams exportParams = new ExportParams("充值订单列表导出(" + orderList.size() + ")", "充值订单列表");

        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, RefillCardOrderVO.class, orderList);
        response.setContentType(new MimetypesFileTypeMap().getContentType("application/octet-stream"));// 设置response内容的类型

        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));// 设置头部信息
        sheets.write(response.getOutputStream());
        sheets.close();

    }


}
